package com.yonyou.cn.mongodbtest.utils;

import org.bson.types.Decimal128;

import java.math.BigDecimal;

/**
 * @ClassName: SssfmNumberUtil
 * @Description: 数值操作工具类
 * @author likun
 * @date 2019-06-13 10:36
 *
 */
public class SssfmNumberUtil {
	
	/**
	 * @Title: addDouble
	 * @Description: double 类型数值相加
	 * @param v
	 * @return
	 */
	public static Double addDouble(Double ...v) {
		BigDecimal b1 = new BigDecimal(v[0]);
		for (int i = 1; i < v.length; i++) {
			BigDecimal b2 = new BigDecimal(v[i]);
			b1 = b1.add(b2);
		}
		return b1.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
	}
	
	/**
	 * @Title: convertDecimal128toDouble
	 * @Description: 将Decimal128 转换为double类型
	 * @param decimal
	 * @return
	 */
	public static Double convertDecimal128toDouble(Decimal128 decimal) {
		if(decimal == null) {
			return null;
		}
		BigDecimal bigDecimalValue = decimal.bigDecimalValue();
		//四舍五入
		return bigDecimalValue.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
	}

	/**
	 * @Title: convertDecimal128toLong
	 * @Description: 将Decimal128 转换为long类型
	 * @param decimal
	 * @return
	 */
	public static Long convertDecimal128toLong(Decimal128 decimal) {
		if(decimal == null) {
			return null;
		}
		BigDecimal bigDecimalValue = decimal.bigDecimalValue();
		return bigDecimalValue.longValue();
	}

//	public static void main(String[] args) {
//		//数字格式化
//		DecimalFormat decimalFormat = new DecimalFormat("0.00");
//		Double addDouble = addDouble(210264.3,294286.02,335214.88,670813.76,882270.06,1049171.5,1322491.52,1573118.68
//				,2350615.02,6233923.3,9541252.94, 16474255.44);
//		String format = decimalFormat.format(addDouble);
//		System.out.println(format);
//	}

}
